package site.xuqing.autohelpertools;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import site.xuqing.autohelperengine.AutoHelper;
import site.xuqing.autohelpertools.service.ScreenService;
import site.xuqing.autohelpertools.util.SharedPreferencesUtil;

public class MainActivity extends AppCompatActivity {
    private Button continueButton;
    private ListView listView;
    private ArrayAdapter<String> arrayAdapter;
    private List<String> screenDataArrays=new ArrayList<>();
    private List<String> arrays=new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button startButton = findViewById(R.id.startButton);
        continueButton=findViewById(R.id.continueButton);
        listView=findViewById(R.id.listView);
        arrayAdapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,arrays);
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    JSONObject jsonObject=new JSONObject(screenDataArrays.get(position));
                    String json=jsonObject.optString("nodes");
                    startActivity(new Intent(MainActivity.this, ScreenDataActivity.class).putExtra("json", json));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferencesUtil.clearScreenData();
                AutoHelper.getInstance().setScreenDataStart(true);
                start();
            }
        });
        continueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AutoHelper.getInstance().setScreenDataStart(true);
                start();
            }
        });

        startService(new Intent(MainActivity.this, ScreenService.class));
    }

    private void start(){
        if (!AutoHelper.getInstance().checkServicePermission(MainActivity.this)) {
            AutoHelper.getInstance().authorize(MainActivity.this,false,true);
        }else{
            onBackPressed();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        AutoHelper.getInstance().setScreenDataStart(false);

        try {
            screenDataArrays = SharedPreferencesUtil.getScreenDataList();
            if (screenDataArrays == null || screenDataArrays.size() <= 0) {
                continueButton.setVisibility(View.GONE);
                listView.setVisibility(View.GONE);
            } else {
                continueButton.setVisibility(View.VISIBLE);
                listView.setVisibility(View.VISIBLE);
                arrays.clear();
                for (String screenData : screenDataArrays) {
                    JSONObject jsonObject=new JSONObject(screenData);
                    arrays.add(jsonObject.optString("className"));
                }
                arrayAdapter.notifyDataSetChanged();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onBackPressed() {
        if (AutoHelper.getInstance().checkServicePermission(this)) {
            AutoHelper.getInstance().setNextScreenNotExecuteOnCurrentScreenDataListener();
            AutoHelper.getInstance().gotoDesktop(this);
        }else{
            super.onBackPressed();
        }
    }
}
